package com.happe.foodeasy.manage.service.impl;

import com.happe.foodeasy.manage.commodity.entity.CommodityTypeEntity;
import com.happe.foodeasy.manage.dao.CommodityBaseMapper;
import com.happe.foodeasy.manage.dto.CommodityBaseDTO;
import com.happe.foodeasy.manage.dto.ImagePhotoDTO;
import com.happe.foodeasy.manage.dto.TypeAndStatusDTO;
import com.happe.foodeasy.manage.commodity.entity.CommodityBaseEntity;
import com.happe.foodeasy.manage.service.CommodityBaseService;
import com.happe.foodeasy.manage.service.CommodityTypeService;
import com.happe.foodeasy.manage.service.ImagePhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommodityBaseServiceImpl implements CommodityBaseService {
    @Autowired
    private CommodityBaseMapper commodityBaseMapper;
    @Autowired
    private ImagePhotoService imagePhotoService;
    @Autowired
    private CommodityTypeService commodityTypeService;

    @Override
    public long selectLastLineId() {
        CommodityBaseEntity entity = commodityBaseMapper.selectLastLine();
        return entity == null ? 0 : entity.getId();
    }

    @Override
    public void save(CommodityBaseDTO commodityBaseDTO, ImagePhotoDTO imagePhotoDTO) {
        commodityBaseMapper.save(commodityBaseDTO);
        imagePhotoService.save(imagePhotoDTO);
    }

    @Override
    public List<CommodityBaseEntity> selectAllByTypeAndStatus(TypeAndStatusDTO typeAndStatusDTO) {
        return commodityBaseMapper.selectAllByTypeAndStatus(typeAndStatusDTO);
    }

    @Override
    public List<CommodityBaseEntity> selectFirstTypeAndStatusValidCommodity() {
        CommodityTypeEntity firstTypeEntity = commodityTypeService.selectFirstValidType();
        TypeAndStatusDTO dto = new TypeAndStatusDTO();
        dto.setCommodityTypeId(firstTypeEntity.getId());
        dto.setCommodityStatusId(1);
        return selectAllByTypeAndStatus(dto);
    }
}
